﻿using AutoMapper;
using ERPSystem.Common.Domains.Notices;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;

namespace SalesManagementWrite.API.SalesManagementWrites.BillingInvoicing
{
    public class BillInvoiceMaterHandler:IRequestHandler<BillInvoiceMaterCommon, Result>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<Billingmaterial> billingmaterialRepository;

        public BillInvoiceMaterHandler(IMapper mapper, IBaseRepository<Billingmaterial> billingmaterialRepository)
        {
            this.mapper = mapper;
            this.billingmaterialRepository = billingmaterialRepository;
        }

        /// <summary>
        /// 开票物料新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(BillInvoiceMaterCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                var list = await billingmaterialRepository.AddRange(request.BillingMaterials);
                if (list > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "开票通知单添加成功";
                }
                else
                {
                    result.Msg = "开票通知单添加失败";
                }
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }
    }
}
